using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NCS.Library.Components;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Zones;
using NCS.Library;

namespace NCS.Module.CMS.Web.HomePage
{
    public partial class NavBar : CMSWebControl
    {
        public DataTable dt = null;
        public DataTable dt1 = null;
        public DataTable dt2 = null;

        protected void Page_Load(object sender, EventArgs e)
        {            
            this.dt = ZoneController.GetZonesVisbleInNavbar(ConvertUtility.ToInt32(AppEnv.PortalId()));
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int parentid = NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Zone_ParentID"].ToString());
                    if (parentid == 0)
                    {
                        int zoneid = NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Zone_ID"].ToString());
                        string link = this.BuildUrlByZoneSubPortal(zoneid, NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Portal_TabControlID"].ToString()));
                        MenuItem item = new MenuItem(dt.Rows[i]["Zone_Name"].ToString(), link);
                        item.NavigateUrl = link;
                        if (parentid != zoneid)
                            this.GetChildrenMenu(dt, item, zoneid);
                        
                        this.Menu1.Items.Add(item);
                    }
                }
            }
        }

        private void GetChildrenMenu(DataTable dt, MenuItem item, int parentid)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int sub_parentid = NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Zone_ParentID"].ToString());
                if (parentid == sub_parentid)
                {
                    int zoneid = NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Zone_ID"].ToString());
                    string link = this.BuildUrlByZoneSubPortal(zoneid, NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Portal_TabControlID"].ToString()));
                    MenuItem subitem = new MenuItem(dt.Rows[i]["Zone_Name"].ToString(), link);
                    subitem.NavigateUrl = link;

                    if (sub_parentid != zoneid)
                        this.GetChildrenMenu(dt, subitem, zoneid);

                    item.ChildItems.Add(subitem);
                }
            }
        }
    }
}